1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.builder;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.apache.commons.lang3.ArrayUtils;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class DiffBuilder implements Builder<DiffResult> {
68
69 private final List<Diff<?>> diffs;
70 private final boolean objectsTriviallyEqual;
71 private final Object left;
72 private final Object right;
73 private final ToStringStyle style;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public DiffBuilder(final Object lhs, final Object rhs,
104 final ToStringStyle style, final boolean testTriviallyEqual) {
105
106 if (lhs == null) {
107 throw new IllegalArgumentException("lhs cannot be null");
108 }
109 if (rhs == null) {
110 throw new IllegalArgumentException("rhs cannot be null");
111 }
112
113 this.diffs = new ArrayList<Diff<?>>();
114 this.left = lhs;
115 this.right = rhs;
116 this.style = style;
117
118
119 this.objectsTriviallyEqual = testTriviallyEqual && (lhs == rhs || lhs.equals(rhs));
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public DiffBuilder(final Object lhs, final Object rhs,
149 final ToStringStyle style) {
150
151 this(lhs, rhs, style, true);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public DiffBuilder append(final String fieldName, final boolean lhs,
170 final boolean rhs) {
171 if (fieldName == null) {
172 throw new IllegalArgumentException("Field name cannot be null");
173 }
174
175 if (objectsTriviallyEqual) {
176 return this;
177 }
178 if (lhs != rhs) {
179 diffs.add(new Diff<Boolean>(fieldName) {
180 private static final long serialVersionUID = 1L;
181
182 @Override
183 public Boolean getLeft() {
184 return Boolean.valueOf(lhs);
185 }
186
187 @Override
188 public Boolean getRight() {
189 return Boolean.valueOf(rhs);
190 }
191 });
192 }
193 return this;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public DiffBuilder append(final String fieldName, final boolean[] lhs,
212 final boolean[] rhs) {
213 if (fieldName == null) {
214 throw new IllegalArgumentException("Field name cannot be null");
215 }
216 if (objectsTriviallyEqual) {
217 return this;
218 }
219 if (!Arrays.equals(lhs, rhs)) {
220 diffs.add(new Diff<Boolean[]>(fieldName) {
221 private static final long serialVersionUID = 1L;
222
223 @Override
224 public Boolean[] getLeft() {
225 return ArrayUtils.toObject(lhs);
226 }
227
228 @Override
229 public Boolean[] getRight() {
230 return ArrayUtils.toObject(rhs);
231 }
232 });
233 }
234 return this;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public DiffBuilder append(final String fieldName, final byte lhs,
253 final byte rhs) {
254 if (fieldName == null) {
255 throw new IllegalArgumentException("Field name cannot be null");
256 }
257 if (objectsTriviallyEqual) {
258 return this;
259 }
260 if (lhs != rhs) {
261 diffs.add(new Diff<Byte>(fieldName) {
262 private static final long serialVersionUID = 1L;
263
264 @Override
265 public Byte getLeft() {
266 return Byte.valueOf(lhs);
267 }
268
269 @Override
270 public Byte getRight() {
271 return Byte.valueOf(rhs);
272 }
273 });
274 }
275 return this;
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public DiffBuilder append(final String fieldName, final byte[] lhs,
294 final byte[] rhs) {
295 if (fieldName == null) {
296 throw new IllegalArgumentException("Field name cannot be null");
297 }
298
299 if (objectsTriviallyEqual) {
300 return this;
301 }
302 if (!Arrays.equals(lhs, rhs)) {
303 diffs.add(new Diff<Byte[]>(fieldName) {
304 private static final long serialVersionUID = 1L;
305
306 @Override
307 public Byte[] getLeft() {
308 return ArrayUtils.toObject(lhs);
309 }
310
311 @Override
312 public Byte[] getRight() {
313 return ArrayUtils.toObject(rhs);
314 }
315 });
316 }
317 return this;
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 public DiffBuilder append(final String fieldName, final char lhs,
336 final char rhs) {
337 if (fieldName == null) {
338 throw new IllegalArgumentException("Field name cannot be null");
339 }
340
341 if (objectsTriviallyEqual) {
342 return this;
343 }
344 if (lhs != rhs) {
345 diffs.add(new Diff<Character>(fieldName) {
346 private static final long serialVersionUID = 1L;
347
348 @Override
349 public Character getLeft() {
350 return Character.valueOf(lhs);
351 }
352
353 @Override
354 public Character getRight() {
355 return Character.valueOf(rhs);
356 }
357 });
358 }
359 return this;
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 public DiffBuilder append(final String fieldName, final char[] lhs,
378 final char[] rhs) {
379 if (fieldName == null) {
380 throw new IllegalArgumentException("Field name cannot be null");
381 }
382
383 if (objectsTriviallyEqual) {
384 return this;
385 }
386 if (!Arrays.equals(lhs, rhs)) {
387 diffs.add(new Diff<Character[]>(fieldName) {
388 private static final long serialVersionUID = 1L;
389
390 @Override
391 public Character[] getLeft() {
392 return ArrayUtils.toObject(lhs);
393 }
394
395 @Override
396 public Character[] getRight() {
397 return ArrayUtils.toObject(rhs);
398 }
399 });
400 }
401 return this;
402 }
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public DiffBuilder append(final String fieldName, final double lhs,
420 final double rhs) {
421 if (fieldName == null) {
422 throw new IllegalArgumentException("Field name cannot be null");
423 }
424
425 if (objectsTriviallyEqual) {
426 return this;
427 }
428 if (Double.doubleToLongBits(lhs) != Double.doubleToLongBits(rhs)) {
429 diffs.add(new Diff<Double>(fieldName) {
430 private static final long serialVersionUID = 1L;
431
432 @Override
433 public Double getLeft() {
434 return Double.valueOf(lhs);
435 }
436
437 @Override
438 public Double getRight() {
439 return Double.valueOf(rhs);
440 }
441 });
442 }
443 return this;
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461 public DiffBuilder append(final String fieldName, final double[] lhs,
462 final double[] rhs) {
463 if (fieldName == null) {
464 throw new IllegalArgumentException("Field name cannot be null");
465 }
466
467 if (objectsTriviallyEqual) {
468 return this;
469 }
470 if (!Arrays.equals(lhs, rhs)) {
471 diffs.add(new Diff<Double[]>(fieldName) {
472 private static final long serialVersionUID = 1L;
473
474 @Override
475 public Double[] getLeft() {
476 return ArrayUtils.toObject(lhs);
477 }
478
479 @Override
480 public Double[] getRight() {
481 return ArrayUtils.toObject(rhs);
482 }
483 });
484 }
485 return this;
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 public DiffBuilder append(final String fieldName, final float lhs,
504 final float rhs) {
505 if (fieldName == null) {
506 throw new IllegalArgumentException("Field name cannot be null");
507 }
508
509 if (objectsTriviallyEqual) {
510 return this;
511 }
512 if (Float.floatToIntBits(lhs) != Float.floatToIntBits(rhs)) {
513 diffs.add(new Diff<Float>(fieldName) {
514 private static final long serialVersionUID = 1L;
515
516 @Override
517 public Float getLeft() {
518 return Float.valueOf(lhs);
519 }
520
521 @Override
522 public Float getRight() {
523 return Float.valueOf(rhs);
524 }
525 });
526 }
527 return this;
528 }
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545 public DiffBuilder append(final String fieldName, final float[] lhs,
546 final float[] rhs) {
547 if (fieldName == null) {
548 throw new IllegalArgumentException("Field name cannot be null");
549 }
550
551 if (objectsTriviallyEqual) {
552 return this;
553 }
554 if (!Arrays.equals(lhs, rhs)) {
555 diffs.add(new Diff<Float[]>(fieldName) {
556 private static final long serialVersionUID = 1L;
557
558 @Override
559 public Float[] getLeft() {
560 return ArrayUtils.toObject(lhs);
561 }
562
563 @Override
564 public Float[] getRight() {
565 return ArrayUtils.toObject(rhs);
566 }
567 });
568 }
569 return this;
570 }
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587 public DiffBuilder append(final String fieldName, final int lhs,
588 final int rhs) {
589 if (fieldName == null) {
590 throw new IllegalArgumentException("Field name cannot be null");
591 }
592
593 if (objectsTriviallyEqual) {
594 return this;
595 }
596 if (lhs != rhs) {
597 diffs.add(new Diff<Integer>(fieldName) {
598 private static final long serialVersionUID = 1L;
599
600 @Override
601 public Integer getLeft() {
602 return Integer.valueOf(lhs);
603 }
604
605 @Override
606 public Integer getRight() {
607 return Integer.valueOf(rhs);
608 }
609 });
610 }
611 return this;
612 }
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629 public DiffBuilder append(final String fieldName, final int[] lhs,
630 final int[] rhs) {
631 if (fieldName == null) {
632 throw new IllegalArgumentException("Field name cannot be null");
633 }
634
635 if (objectsTriviallyEqual) {
636 return this;
637 }
638 if (!Arrays.equals(lhs, rhs)) {
639 diffs.add(new Diff<Integer[]>(fieldName) {
640 private static final long serialVersionUID = 1L;
641
642 @Override
643 public Integer[] getLeft() {
644 return ArrayUtils.toObject(lhs);
645 }
646
647 @Override
648 public Integer[] getRight() {
649 return ArrayUtils.toObject(rhs);
650 }
651 });
652 }
653 return this;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671 public DiffBuilder append(final String fieldName, final long lhs,
672 final long rhs) {
673 if (fieldName == null) {
674 throw new IllegalArgumentException("Field name cannot be null");
675 }
676
677 if (objectsTriviallyEqual) {
678 return this;
679 }
680 if (lhs != rhs) {
681 diffs.add(new Diff<Long>(fieldName) {
682 private static final long serialVersionUID = 1L;
683
684 @Override
685 public Long getLeft() {
686 return Long.valueOf(lhs);
687 }
688
689 @Override
690 public Long getRight() {
691 return Long.valueOf(rhs);
692 }
693 });
694 }
695 return this;
696 }
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713 public DiffBuilder append(final String fieldName, final long[] lhs,
714 final long[] rhs) {
715 if (fieldName == null) {
716 throw new IllegalArgumentException("Field name cannot be null");
717 }
718
719 if (objectsTriviallyEqual) {
720 return this;
721 }
722 if (!Arrays.equals(lhs, rhs)) {
723 diffs.add(new Diff<Long[]>(fieldName) {
724 private static final long serialVersionUID = 1L;
725
726 @Override
727 public Long[] getLeft() {
728 return ArrayUtils.toObject(lhs);
729 }
730
731 @Override
732 public Long[] getRight() {
733 return ArrayUtils.toObject(rhs);
734 }
735 });
736 }
737 return this;
738 }
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755 public DiffBuilder append(final String fieldName, final short lhs,
756 final short rhs) {
757 if (fieldName == null) {
758 throw new IllegalArgumentException("Field name cannot be null");
759 }
760
761 if (objectsTriviallyEqual) {
762 return this;
763 }
764 if (lhs != rhs) {
765 diffs.add(new Diff<Short>(fieldName) {
766 private static final long serialVersionUID = 1L;
767
768 @Override
769 public Short getLeft() {
770 return Short.valueOf(lhs);
771 }
772
773 @Override
774 public Short getRight() {
775 return Short.valueOf(rhs);
776 }
777 });
778 }
779 return this;
780 }
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797 public DiffBuilder append(final String fieldName, final short[] lhs,
798 final short[] rhs) {
799 if (fieldName == null) {
800 throw new IllegalArgumentException("Field name cannot be null");
801 }
802
803 if (objectsTriviallyEqual) {
804 return this;
805 }
806 if (!Arrays.equals(lhs, rhs)) {
807 diffs.add(new Diff<Short[]>(fieldName) {
808 private static final long serialVersionUID = 1L;
809
810 @Override
811 public Short[] getLeft() {
812 return ArrayUtils.toObject(lhs);
813 }
814
815 @Override
816 public Short[] getRight() {
817 return ArrayUtils.toObject(rhs);
818 }
819 });
820 }
821 return this;
822 }
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837 public DiffBuilder append(final String fieldName, final Object lhs,
838 final Object rhs) {
839
840 if (objectsTriviallyEqual) {
841 return this;
842 }
843 if (lhs == rhs) {
844 return this;
845 }
846
847 Object objectToTest;
848 if (lhs != null) {
849 objectToTest = lhs;
850 } else {
851
852 objectToTest = rhs;
853 }
854
855 if (objectToTest.getClass().isArray()) {
856 if (objectToTest instanceof boolean[]) {
857 return append(fieldName, (boolean[]) lhs, (boolean[]) rhs);
858 }
859 if (objectToTest instanceof byte[]) {
860 return append(fieldName, (byte[]) lhs, (byte[]) rhs);
861 }
862 if (objectToTest instanceof char[]) {
863 return append(fieldName, (char[]) lhs, (char[]) rhs);
864 }
865 if (objectToTest instanceof double[]) {
866 return append(fieldName, (double[]) lhs, (double[]) rhs);
867 }
868 if (objectToTest instanceof float[]) {
869 return append(fieldName, (float[]) lhs, (float[]) rhs);
870 }
871 if (objectToTest instanceof int[]) {
872 return append(fieldName, (int[]) lhs, (int[]) rhs);
873 }
874 if (objectToTest instanceof long[]) {
875 return append(fieldName, (long[]) lhs, (long[]) rhs);
876 }
877 if (objectToTest instanceof short[]) {
878 return append(fieldName, (short[]) lhs, (short[]) rhs);
879 }
880
881 return append(fieldName, (Object[]) lhs, (Object[]) rhs);
882 }
883
884
885 if (lhs != null && lhs.equals(rhs)) {
886 return this;
887 }
888
889 diffs.add(new Diff<Object>(fieldName) {
890 private static final long serialVersionUID = 1L;
891
892 @Override
893 public Object getLeft() {
894 return lhs;
895 }
896
897 @Override
898 public Object getRight() {
899 return rhs;
900 }
901 });
902
903 return this;
904 }
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919 public DiffBuilder append(final String fieldName, final Object[] lhs,
920 final Object[] rhs) {
921 if (objectsTriviallyEqual) {
922 return this;
923 }
924
925 if (!Arrays.equals(lhs, rhs)) {
926 diffs.add(new Diff<Object[]>(fieldName) {
927 private static final long serialVersionUID = 1L;
928
929 @Override
930 public Object[] getLeft() {
931 return lhs;
932 }
933
934 @Override
935 public Object[] getRight() {
936 return rhs;
937 }
938 });
939 }
940
941 return this;
942 }
943
944
945
946
947
948
949
950
951
952
953 @Override
954 public DiffResult build() {
955 return new DiffResult(left, right, diffs, style);
956 }
957
958 }